@echo off

rem Перезапускает текущий домен с очисткой полтавских модулей в applications, статусов autodeploy, содержимого generated и osgi-cache

set BUILD_TIME=%TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%
set BUILD_DATE=%DATE:~4,2% %DATE:~7,2%
echo -- RUNNING RESTART SCRIPT %BUILD_TIME% %BUILD_DATE%

set domain_dir=%1
if "%domain_dir%"=="" (set domain_dir=%~dp0)
IF %domain_dir:~-1%==\ SET domain_dir=%domain_dir:~0,-1%

for %%I in ("%domain_dir%") do set domain=%%~nI

if "%domain%"=="" (
	echo Domain name was not specified
	exit 1
)

echo Domain name is %domain%
echo Domain dir is %domain_dir%

set BIN_DIR=%domain_dir%\..\..\..\bin
echo Stopping domain %domain%
call %BIN_DIR%\asadmin.bat stop-domain %domain%

echo -- Removing unpacked poltava modules from %domain_dir%\applications
for /D %%G in (%domain_dir%\applications\poltava*) do (
	rd /s /q "%%G"
	echo %%G
)
echo ==========================
echo -- Removing status files from %domain_dir%\autodeploy
for %%G in (%domain_dir%\autodeploy\*) do (
	if %%~zG == 0 (
		del "%%G"
		echo %%G
	)
)
echo ==========================
echo -- Removing caches from %domain_dir%\generated
for /D %%G in (%domain_dir%\generated\*) do (
	rd /s /q "%%G"
	echo %%G
)
echo ==========================
echo -- Removing osgi-cache from %domain_dir%\osgi-cache
if exist %domain_dir%\osgi-cache\felix (
	rd /s /q %domain_dir%\osgi-cache\felix
	echo %domain_dir%\osgi-cache\felix
)

echo Starting domain %domain%
call %BIN_DIR%\asadmin.bat start-domain %domain%
exit 0